<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="时段：" name="timeslotId">
						<a-select
							v-model:value="searchFormState.timeslotId"
							show-search
							:options="timeslotData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择时段"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="教师" name="teacher">
						<a-input v-model:value="searchFormState.teacher" placeholder="请输入" />
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="课程" name="subject">
						<a-input v-model:value="searchFormState.subject" placeholder="请输入" />
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="班级" name="studentGroup">
						<a-input v-model:value="searchFormState.studentGroup" placeholder="请输入" />
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="教室：" name="roomId">
						<a-select
							v-model:value="searchFormState.roomId"
							show-search
							:options="roomData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择教室"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="状态：" name="locked">
						<a-select
							v-model:value="searchFormState.locked"
							:options="statusData"
							:field-names="{ label: 'name', value: 'value' }"
							style="width: 100%"
							placeholder="请选择"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button @click="updateLesson()">
						<template #icon><reload-outlined /></template>
						批量更新
					</a-button>
					<a-button @click="lockLesson()">
						<template #icon><lock-outlined /></template>
						批量锁定
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('eduLessonBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchEduLesson"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'locked'">
					{{ record.locked ? "已锁定":"未锁定" }}
				</template>
				<template v-if="column.dataIndex === 'startWeekEndWeek'">
					{{ record.startWeek + "-" + record.endWeek }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('eduLessonEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['eduLessonEdit', 'eduLessonDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteEduLesson(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('eduLessonDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="lesson">
	import Form from './form.vue'
	import {message} from "ant-design-vue";
	import eduTimeslotApi from '@/api/biz/eduTimeslotApi'
	import eduRoomApi from '@/api/biz/eduRoomApi'
	import eduTeacherApi from '@/api/biz/eduTeacherApi'
	import eduLessonApi from '@/api/biz/eduLessonApi'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 时段数据
	let timeslotData = ref([])
	// 教室数据
	let roomData = ref([])
	// 状态数据
	let statusData = ref([
		{
			name: '已锁定',
			value: true
		},
		{
			name: '未锁定',
			value: false
		}
	])
	// 教师数据
	let teacherData = ref([])
	const columns = [
		{
			title: '时段',
			dataIndex: 'timeslotName'
		},
		{
			title: '起止周',
			dataIndex: 'startWeekEndWeek'
		},
		{
			title: '教室',
			dataIndex: 'roomName'
		},
		{
			title: '教师',
			dataIndex: 'teacher'
		},
		{
			title: '课程',
			dataIndex: 'subject'
		},
		{
			title: '班级',
			dataIndex: 'studentGroup'
		},
		{
			title: '人数',
			dataIndex: 'lectureSize'
		},
		{
			title: '状态',
			dataIndex: 'locked'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduLessonEdit', 'eduLessonDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		seleTimeslotData()
		seleRoomData()
		seleTeacherData()
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduLessonApi.eduLessonPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 选择时段
	const seleTimeslotData = () => {
		const param = {}
		eduTimeslotApi.eduTimeslotList(param).then((res) => {
			timeslotData.value = res
		})
	}
	// 选择教室
	const seleRoomData = () => {
		const param = {}
		eduRoomApi.eduRoomList(param).then((res) => {
			roomData.value = res
		})
	}
	// 选择教师
	const seleTeacherData = () => {
		const param = {}
		eduTeacherApi.eduTeacherList(param).then((res) => {
			teacherData.value = res
		})
	}
	// 过滤选项
    const filterOption = (input, option) => {
		return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteEduLesson = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduLessonApi.eduLessonDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量更新
	const updateLesson = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择作为更新模版的记录')
			return
		}
		let params = selectedRowKeys.value.map((id) => {
			return { id: id };
		});
		eduLessonApi.eduLessonUpdate(params).then(() => {
			message.success('操作成功');
			table.value.clearSelected();
			table.value.refresh(true);
		});
	}
	// 批量锁定
	const lockLesson = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择要锁定的记录')
			return
		}
		let params = selectedRowKeys.value.map((id) => {
			return { id: id };
		});
		eduLessonApi.eduLessonLock(params).then(() => {
			message.success('操作成功');
			table.value.clearSelected();
			table.value.refresh(true);
		});
	}
	// 批量删除
	const deleteBatchEduLesson = (params) => {
		eduLessonApi.eduLessonDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>
